package com.chaos.system.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.ChainQuery;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.entity.Table;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.model.helper.PageHelper;
import com.chaos.system.api.data.RbacUserData;
import com.chaos.system.api.dto.RbacUserDto;
import com.chaos.system.dto.RbacUserRolesDto;
import com.chaos.system.entity.RbacUser;
import com.chaos.system.mapper.RbacUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

/**
 * @author Jacky.Cui
 * @since 2024-11-13
 */
@Slf4j
@Component
public class RbacUserService extends ServiceImpl<RbacUserMapper, RbacUser> {

    public ID insertModel(RbacUserData data) {
        RbacUser entity = ConvertHelper.convert(data, RbacUser.class);
        return save(entity) ? new ID(entity.getId()) : null;
    }

    public boolean deleteModel(ID data) {
        return removeById(data.getId());
    }

    public boolean updateModelByID(RbacUserData data) {
        RbacUser entity = ConvertHelper.convert(data, RbacUser.class);
        return update(entity, new UpdateWrapper<RbacUser>().eq(Table.ID, data.getId()));
    }

    public RbacUserData selectByID(ID data) {
        return ConvertHelper.convert(getOne(new QueryWrapper<RbacUser>().eq(Table.ID, data.getId())), RbacUserData.class);
    }

    public List<RbacUserData> selectByData(RbacUserData data) {
        LambdaQueryWrapper<RbacUser> query = new LambdaQueryWrapper<>();
        query.eq(!ObjectUtil.isEmpty(data.getMobile()), RbacUser::getMobile, data.getMobile())
                .eq(!ObjectUtil.isEmpty(data.getUsername()), RbacUser::getUsername, data.getUsername())
                .eq(!ObjectUtil.isEmpty(data.getTenantId()), RbacUser::getTenantId, data.getTenantId())
        ;
        query.orderByDesc(RbacUser::getId);
        return ConvertHelper.convert(list(query), RbacUserData.class);
    }

    public PageList<RbacUserData> selectByPage(PageQueryDto<RbacUserData> pageData) {
        RbacUserData data = pageData.getData();
        LambdaQueryWrapper<RbacUser> query = new LambdaQueryWrapper<>();
        query.eq(!ObjectUtil.isEmpty(data.getId()), RbacUser::getId, data.getId()).eq(!ObjectUtil.isEmpty(data.getUsername()), RbacUser::getUsername, data.getUsername())
                .eq(!ObjectUtil.isEmpty(data.getStatus()), RbacUser::getStatus, data.getStatus())
                .eq(!ObjectUtil.isEmpty(data.getTenantId()), RbacUser::getTenantId, data.getTenantId());
        query.orderByDesc(RbacUser::getId);

        return PageHelper.pageList(page(PageHelper.page(pageData), query), RbacUserData.class);
    }

    public RbacUserData selectByMobile(String phone) {
        return ConvertHelper.convert(getOne(new LambdaQueryWrapper<RbacUser>().eq(RbacUser::getMobile, phone)), RbacUserData.class);
    }


    public PageList<RbacUserRolesDto> rolePage(PageQueryDto<RbacUserData> pageData) {
        IPage<RbacUserRolesDto> page = baseMapper.rolePage(PageHelper.page(pageData), pageData.getData());
        return PageHelper.pageList(page);

    }


}
